/*
 *  Copyright (C) 2025 ameise <ameise.wang@gmail.com> - All Rights Reserved
 *
 *  This file is part of e3net.
 *
 *  e3net is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  e3net is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with e3net. If not, see <https://www.gnu.org/licenses/>.
 */

package e3net

type Conn interface {
	ID() uint64
	Tag() Tag
	SetContext(ctx ConnContext)
	Context() ConnContext
	LocalAddr() string
	RemoteAddr() string
	SendSync([]byte) error //在收到OnSendOver消息前,send对象不可释放,不可复用
	SendAsync([]byte) error
	IsConnected() bool
	Close() error
	TrafficData() string
}

type ConnContext interface {
	OnRecv(Conn, []byte) ([]byte, error) //OnRecv的数据不可交给其他对象复用.如需复用请自行拷贝
}

type AcceptHandler interface {
	OnNewConnect(Tag, Conn) ConnContext //连接请求回调
	OnClosedConnect(ConnContext)
}

type TcpServer interface {
	Start() error                                                                                                 //启动网络服务器
	Stop()                                                                                                        //停止网络服务
	AddAcceptor(hand AcceptHandler, tag Tag, addr string, pause bool, ops ...*TcpAcceptorOptions) (string, error) //添加监听器
	RemoveAcceptor(tag Tag)                                                                                       //移除指定监听器,并清理其连接
	DisConnectByTag(tag Tag, pause bool)                                                                          //断开指定监听器的连接,并设置暂停接受连接
	DisConnectAll(pause bool)                                                                                     //断开所有连接,并设置暂停接受连接
	PauseAcceptByTag(tag Tag)                                                                                     //暂停指定监听器接受连接
	PauseAccept()                                                                                                 //暂停所有监听器接受连接
	ResumeAcceptByTag(tag Tag)                                                                                    //恢复指定监听器接受连接
	ResumeAccept()                                                                                                //恢复所有监听器接受连接
}

type TcpAgentHandler interface {
	OnAgentConnect(agent TcpAgent, ctx ConnContext, conn Conn)
	OnAgentConnectFailed(agent TcpAgent, tag Tag, ctx ConnContext, err error)
	OnAgentClosedConn(agent TcpAgent, tag Tag, ctx ConnContext)
}

type TcpAgent interface {
	Start() error
	Stop()
	ConnectSync(tag Tag, ctx ConnContext, addr string) (Conn, error)
	ConnectAsync(tag Tag, ctx ConnContext, addr string) error
	DisConnectAll()
}
